package com.zufe.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    //定义一个二维数组
    //用来存储图片的编号
    int[][] data = new int[4][4];
    //定义一个空白块的位置
    int posx, posy;
    String path = "day16_code\\image\\animal\\animal3\\";
    int[][] win = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    int step = 0;

    //设置菜单项
    JMenuItem replayItem;
    JMenuItem reLoginItem;
    JMenuItem closeItem;
    JMenuItem accountItem;
    JMenuItem girl;
    JMenuItem animal;
    JMenuItem sport;


    public GameJFrame() {
        //初始化界面
        initJFrame();

        //初始化菜单
        initJMenuBar();

        //初始化数据,也是绘制当前界面
        initData();
        //初始化图片
        initImage();

        //设置可见
        this.setVisible(true);
    }

    private void initData() {
        int[] tempArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

        //打乱数组
        Random r = new Random();
        for (int i = 0; i < 16; i++) {
            int index = r.nextInt(16);
            int temp = tempArray[i];
            tempArray[i] = tempArray[index];
            tempArray[index] = temp;
        }

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (tempArray[i * 4 + j] == 0) {
                    posx = i;
                    posy = j;
                }//记录空白块的位置
                data[i][j] = tempArray[i * 4 + j];
            }
        }
    }

    private void initImage() {
        this.getContentPane().removeAll();
        //设置步数
        JLabel jLabel1 = new JLabel("步数: " + step);
        jLabel1.setBounds(50, 30, 100, 20);
        this.getContentPane().add(jLabel1);
        if(victory()){
            JLabel jLabel = new JLabel(new ImageIcon("day16_code/image/win.png"));
            jLabel.setBounds(203,283, 197, 73);
            this.getContentPane().add(jLabel);
        }
        //循环遍历二维数组
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                JLabel jLabel = new JLabel(new ImageIcon(path + data[i][j] + ".jpg"));
                jLabel.setBounds(j * 105 + 83, i * 105 + 134, 105, 105);
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                this.getContentPane().add(jLabel);
            }
        }
        //设置背景
        JLabel background = new JLabel(new ImageIcon("day16_code/image/background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);
        //重新绘制
        this.getContentPane().repaint();
    }

    private void initJFrame() {
        //设置大小
        this.setSize(603, 680);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置标题
        this.setTitle("拼图游戏 v1.0");
        //设置关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置布局,取消默认布局
        this.setLayout(null);
        //监听键盘
        this.addKeyListener(this);
    }

    private void initJMenuBar() {
        //设置菜单
        JMenuBar jMenuBar = new JMenuBar();
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");

        //创建更换图片
        JMenu changeImage = new JMenu("更换图片");

        //设置菜单项
        replayItem = new JMenuItem("重新游戏");
        reLoginItem = new JMenuItem("重新登录");
        closeItem = new JMenuItem("关闭游戏");

        girl = new JMenuItem("美女");
        animal = new JMenuItem("动物");
        sport = new JMenuItem("运动");

        //4.把美女，动物，运动添加到更换图片当中
        changeImage.add(girl);
        changeImage.add(animal);
        changeImage.add(sport);

        //添加监听
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        girl.addActionListener(this);
        animal.addActionListener(this);
        sport.addActionListener(this);

        accountItem = new JMenuItem("公众号");
        //添加监听
        accountItem.addActionListener(this);

        //添加菜单项
        functionJMenu.add(changeImage);
        functionJMenu.add(replayItem);
        functionJMenu.add(reLoginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);


        //添加菜单
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        //设置菜单
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);


        //设置菜单
        this.setJMenuBar(jMenuBar);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_A){
            this.getContentPane().removeAll();
            JLabel jLabel = new JLabel(new ImageIcon(path + "all.jpg"));
            jLabel.setBounds(83,134, 420, 420);
            this.getContentPane().add(jLabel);
            //设置背景
            JLabel background = new JLabel(new ImageIcon("day16_code/image/background.png"));
            background.setBounds(40, 40, 508, 560);
            this.getContentPane().add(background);
            this.getContentPane().repaint();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victory()){
            return;
        }
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP -> {
                if (posx == 3) {
                    return;
                }
                int temp = data[posx][posy];
                data[posx][posy] = data[posx + 1][posy];
                data[posx + 1][posy] = temp;
                posx++;
                step++;
                initImage();
            }
            case KeyEvent.VK_DOWN -> {
                if (posx == 0) {
                    return;
                }
                int temp = data[posx][posy];
                data[posx][posy] = data[posx - 1][posy];
                data[posx - 1][posy] = temp;
                posx--;
                step++;
                initImage();
            }
            case KeyEvent.VK_LEFT -> {
                if(posy == 3){
                    return;
                }
                int temp = data[posx][posy];
                data[posx][posy] = data[posx][posy + 1];
                data[posx][posy + 1] = temp;
                posy++;
                step++;
                initImage();
            }
            case KeyEvent.VK_RIGHT -> {
                if(posy == 0){
                    return;
                }
                int temp = data[posx][posy];
                data[posx][posy] = data[posx][posy - 1];
                data[posx][posy - 1] = temp;
                posy--;
                step++;
                initImage();
            }
            case KeyEvent.VK_A -> {
                initImage();
            }
            case KeyEvent.VK_W -> {
                data = win;
                initImage();
            }
        }
    }
    public  boolean victory(){
        for (int i = 0; i < data.length; i++) {
            for(int j = 0; j < data[0].length; j++){
                if(data[i][j] != win[i][j]){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == replayItem){
            step = 0;
            initData();
            initImage();
        }
        if (e.getSource() == reLoginItem){
            this.dispose();
            LoginJFrame loginJFrame = new LoginJFrame();
        }
        if(e.getSource() == closeItem){
            System.exit(0);
        }
        if(e.getSource() == accountItem){
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("day16_code/image/about.png"));
            jLabel.setBounds(0, 0, 258, 258);
            jDialog.add(jLabel);
            jDialog.setSize(344, 344);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        }
        if(e.getSource() == girl){
            Random r = new Random();
            int index = r.nextInt(13) + 1;
            //设置路径
            path = "day16_code\\image\\girl\\girl" + index + "\\";
            step = 0;
            //初始化数据
            initData();
            //初始化图片
            initImage();
        }
        if(e.getSource() == animal){
            Random r = new Random();
            int index = r.nextInt(8) + 1;
            //设置路径
            path = "day16_code\\image\\animal\\animal" + index + "\\";
            step = 0;
            //初始化数据
            initData();
            //初始化图片
            initImage();
        }
        if(e.getSource() == sport){
            Random r = new Random();
            int index = r.nextInt(10) + 1;
            //设置路径
            path = "day16_code\\image\\sport\\sport" + index + "\\";
            step = 0;
            //初始化数据
            initData();
            //初始化图片
            initImage();
        }
    }
}
